package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    @Override
    public void save(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
    }

    @Override
    public void deleteById(Long id) {

        //调用mapper查询当前分类下菜品数量,如果有,返回错误
        Integer number1 = categoryMapper.countByDishCategoryId(id);
        if (number1 > 0) {
            //使用自定义异常的方法 当此id下菜品数据大于0的时候 抛出异常CustomException
            //全局异常处理器接收这个异常会将异常信息返回给前端
            throw new CustomException("此分类下存在菜品,不允许删除");
        }
        //调用mapper查询当前分类下套餐数量,如果有,返回错误
        Integer number2 = categoryMapper.countBySetCategorymeaID(id);
        if (number2 > 0) {
            throw new CustomException("此分类下存在套餐品,不允许删除");
        }
        //根据id删除
        categoryMapper.deleteByid(id);

    }

    @Override
    public List<Category> findByType(Integer type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType, type);
        List<Category> categoryList = categoryMapper.selectList(wrapper);

        return categoryList;
    }
  /*  @Override
    public ResultInfo deleteById(Long id) {

        //调用mapper查询当前分类下菜品数量,如果有,返回错误
        Integer number1 = categoryMapper.countByDishCategoryId(id);
        if (number1 > 0) {
            return ResultInfo.error("此分类下存在菜品,不允许删除");
        }
        //调用mapper查询当前分类下套餐数量,如果有,返回错误
        Integer number2 = categoryMapper.countBySetCategorymeaID(id);
        if (number2 > 0) {
            return ResultInfo.error("此分类下存在套餐品,不允许删除");
        }
        //根据id删除
        categoryMapper.deleteByid(id);
        return ResultInfo.success(null);
    }*/
}
